package com.furniture.ui.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;

import com.furniture.R;
import com.furniture.Utils;
import com.furniture.sql.Good;
import com.furniture.ui.utils.data.QiNiuUpLoad;
import com.furniture.sql.manage.GoodManage;
import com.furniture.ui.UpdateUi;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Bob on 2016/3/28.
 */
public class PublishActivity extends BaseActivity implements UpdateUi {
    public static final String ARG_TAKEN_PHOTO_URI = "arg_taken_photo_uri";
    private List<Good> goodList = new ArrayList<Good>();
    private GoodManage goodManage = GoodManage.newInstance(this);

    @Bind(R.id.ivPhoto)
    ImageView ivPhoto;
    @Bind(R.id.etDescription)
    EditText etDescription;
    @Bind(R.id.etGetPrice)
    EditText etGetPrice;

    private Uri photoUri;
    private int photoSize;

    public static void openWithPhotoUri(Activity openingActivity, Uri photoUri) {
        Intent intent = new Intent(openingActivity, PublishActivity.class);
        intent.putExtra(ARG_TAKEN_PHOTO_URI, photoUri);
        openingActivity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_grey600_24dp);
        photoSize = getResources().getDimensionPixelSize(R.dimen.publish_photo_thumbnail_size);
        //显示图片
        if (savedInstanceState == null) {
            photoUri = getIntent().getParcelableExtra(ARG_TAKEN_PHOTO_URI);
            Log.e("message", photoUri.toString());
        } else {
            photoUri = savedInstanceState.getParcelable(ARG_TAKEN_PHOTO_URI);
        }
        updateStatusBarColor();

        ivPhoto.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                ivPhoto.getViewTreeObserver();
                loadThumbnailPhoto();
                return true;
            }
        });

        goodList = goodManage.showGoodList();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void updateStatusBarColor() {
        if (Utils.isAndroid5()) {
            getWindow().setStatusBarColor(0xff888888);
        }
    }

    private void loadThumbnailPhoto() {
//        ivPhoto.setScaleX(0);
//        ivPhoto.setScaleY(0);
//        Picasso.with(this)
//                .load(photoUri)
//                .centerCrop()
//                .resize(photoSize, photoSize)
//                .into(ivPhoto, new Callback() {
//                    @Override
//                    public void onSuccess() {
//                        ivPhoto.animate()
//                                .scaleX(1.f).scaleY(1.f)
//                                .setInterpolator(new OvershootInterpolator())
//                                .setDuration(400)
//                                .setStartDelay(200)
//                                .start();
//                    }
//
//                    @Override
//                    public void onError() {
//                        Toast.makeText(getApplicationContext(),"找不到你的照片",Toast.LENGTH_SHORT).show();
//                    }
//                });
        ivPhoto.setImageURI(photoUri);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_publish, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_publish) {
            //写入本地数据库
            Good good = new Good();
            String real_url = photoUri.toString().substring(photoUri.toString().indexOf("DCIM/")).replace("DCIM/","");
            good.setId((long) goodList.size() + 1);
            good.setGoodMsg(etDescription.getText().toString());
            if (etGetPrice.getText().length() != 0){
                good.setLikeNum(Long.parseLong(etGetPrice.getText().toString()));
            }else {
                good.setLikeNum((long) 0);
            }
            Log.e("message",etGetPrice.getText().toString());
            good.setGoodPic("http://7xs9ef.com1.z0.glb.clouddn.com/" + real_url);
            good.setIcon("http://7xs9ef.com1.z0.glb.clouddn.com/logo.jpg");
            good.setGoodUrl("http://7xs9ef.com1.z0.glb.clouddn.com/" + real_url);
            good.setNickName("Bob");
            good.setIsLike(false);
            good.setUserId((long) 10000);

            goodManage.addItem(good);
            //上传图片
            new QiNiuUpLoad(real_url, this);
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    private void bringMainActivityToTop() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(ARG_TAKEN_PHOTO_URI, photoUri);
    }

    @Override
    public void onUpdate() {
        bringMainActivityToTop();
    }
}
